package com.example.program.demos.controller;

import com.example.program.demos.request.ProgramParams;
import com.example.program.demos.service.ProgramService;
import com.example.program.demos.utils.JsonUtil;
import com.example.program.exception.ErrorRes;
import com.sun.corba.se.spi.ior.ObjectKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/program")
public class ProgramController {

    @Autowired
    ProgramService programService;
    @RequestMapping(value = "/selectCompanys", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String selectCompanys(HttpServletRequest request) {
        List<Map<String, Object>> list = programService.selectCompanys();
        return JsonUtil.writeObjectAsString(list);
    }

    @RequestMapping(value = "/selectPrograms", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String selectAll(HttpServletRequest request) throws ParseException {
        String id = request.getParameter("companyId");
        int companyId = Integer.parseInt(id);
        List<Map<String, Object>> list = programService.selectPrograms(companyId);
        return JsonUtil.writeObjectAsString(list);
    }

    @RequestMapping(value = "/insertCompany", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public ErrorRes insertCompany(@RequestBody Map<String, Object> companys) throws ParseException {
        String company = companys.get("company").toString();
        try {
            programService.insertCompany(company);
            return new ErrorRes("新增成功",0);
        } catch (Exception e) {
            return new ErrorRes(e.getCause().getMessage(), -1);
        }
    }

    @RequestMapping(value = "/insertPrograms", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public ErrorRes insertPrograms(@RequestBody ProgramParams programInf) {
        Map<String, Object> map = new HashMap<>();
        map.put("companyId", programInf.getCompanyId());
        map.put("program", programInf.getProgram());
        try {
            programService.insertPrograms(map);
            return new ErrorRes("新增成功",0);
        } catch (Exception e) {
            return new ErrorRes(e.getCause().getMessage(), -1);
        }
    }


    @RequestMapping(value = "/selectGroups", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String selectGroups(HttpServletRequest request) throws ParseException {
        String id = request.getParameter("programId");
        int programId = Integer.parseInt(id);
        List<Map<String, Object>> list = programService.selectGroups(programId);
        return JsonUtil.writeObjectAsString(list);
    }
}
